class Solution {
public:
    vector<int> replaceElements(vector<int>& arr) {
        int pre = arr[arr.size() - 1];
        arr[arr.size() - 1] = -1;
        for (int i = arr.size() - 2, x; i >= 0; i--) {
            x = arr[i];
            arr[i] = pre;
            pre = max(x, pre);
        }
        return arr;
    }
};
